package cn.songhaiqing.walle.help;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.util.Calendar;

import cn.songhaiqing.walle.bean.ImgVersion;
import cn.songhaiqing.walle.global.Global;

public class VersionHelper {
    public static final String ACTION_CLICK_UPDATE_IMG_VERSION = "com.gzgamut.smart_movement..action_click_update_img_version";
    public static final boolean DEF_AUTO_CHECK_VERSION = false;
    public static final String DEF_VERSION_CODE_WATCH = "--";
    public static final String KEY_AUTO_CHECK_VERSION = "KEY_AUTO_CHECK_VERSION";
    public static final String KEY_VERSION_WATCH = "KEY_VERSION_WATCH";
    public static final String TYPE_VERSION_H03 = "H03";
    public static final String TYPE_VERSION_I10 = "I10";
    public static final String TYPE_VERSION_I3 = "I3";
    public static final String TYPE_VERSION_I7P = "I7+";
    public static final String TYPE_VERSION_I8 = "I8";
    public static final String TYPE_VERSION_SH06 = "SH06";
    public static final String TYPE_VERSION_SH07 = "SH07";
    public static final String TYPE_VERSION_SH08 = "SH08";
    public static final String TYPE_VERSION_SH09 = "SH09";
    public static final String TYPE_VERSION_SH09U = "SH09U";

    public static void setVersionWatch(String version) {
        SpHelper.putString(KEY_VERSION_WATCH + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], version);
    }

    public static String getVersionWatch() {
        return SpHelper.getString(KEY_VERSION_WATCH + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], DEF_VERSION_CODE_WATCH);
    }

    public static void setAutoCheckVersion(boolean isAuto) {
        SpHelper.putBoolean(KEY_AUTO_CHECK_VERSION + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], isAuto);
    }

    public static boolean getAutoCheckVersion() {
        return SpHelper.getBoolean(KEY_AUTO_CHECK_VERSION + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], false).booleanValue();
    }

    public static String getVersionName(Context context) {
        PackageInfo packInfo = null;
        try {
            packInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packInfo.versionName;
    }

    public static String getVersionFromServer(String path) {
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
          //  conn.setConnectTimeout(DisconnectedBufferOptions.DISCONNECTED_BUFFER_SIZE_DEFAULT);
            InputStream is = conn.getInputStream();
            String status = new BufferedReader(new InputStreamReader(is)).readLine();
            if (status != null) {
                return status;
            }
            is.close();
            return status;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean isNewVersion(String currentVersion, String serviceVersion) {
        String[] currentCode = currentVersion.replace("v", "").split("\\.");
        String[] serviceCode = serviceVersion.replace("v", "").split("\\.");
        int length = serviceCode.length;
        if (currentCode.length != length || length <= 0) {
            return false;
        }
        if (Integer.parseInt(serviceCode[0]) > Integer.parseInt(currentCode[0])) {
            return true;
        }
        if (Integer.parseInt(serviceCode[0]) != Integer.parseInt(currentCode[0])) {
            return Integer.parseInt(serviceCode[0]) < Integer.parseInt(currentCode[0]) ? false : false;
        }
        if (Integer.parseInt(serviceCode[1]) > Integer.parseInt(currentCode[1])) {
            return true;
        }
        if (Integer.parseInt(serviceCode[1]) != Integer.parseInt(currentCode[1])) {
            return Integer.parseInt(serviceCode[1]) < Integer.parseInt(currentCode[1]) ? false : false;
        }
        if (Integer.parseInt(serviceCode[2]) > Integer.parseInt(currentCode[2])) {
            return true;
        }
        if (Integer.parseInt(serviceCode[2]) == Integer.parseInt(currentCode[2])) {
            return false;
        }
        return Integer.parseInt(serviceCode[2]) < Integer.parseInt(currentCode[2]) ? false : false;
    }

    public static ImgVersion parserImgVersion(String version) {
        ImgVersion imgVersion = new ImgVersion();
        if (version != null) {
            String[] versionArray = version.split("\\.");
            if (versionArray.length == 3) {
                Calendar cal = Calendar.getInstance();
                try {
                    cal.setTime(FormatHelper.sdf_yyyyMMdd.parse(versionArray[0]));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                imgVersion.setDate(cal);
                imgVersion.setType(versionArray[1]);
                imgVersion.setNumber(versionArray[2]);
                return imgVersion;
            } else if (versionArray.length == 4) {
                Calendar cal2 = Calendar.getInstance();
                try {
                    cal2.setTime(FormatHelper.sdf_yyyyMMdd.parse(versionArray[0]));
                } catch (ParseException e2) {
                    e2.printStackTrace();
                }
                imgVersion.setDate(cal2);
                imgVersion.setType(versionArray[1]);
                imgVersion.setNumber(versionArray[2]);
                imgVersion.setVersion(versionArray[3]);
                return imgVersion;
            }
        }
        return null;
    }

    public static void checkVersionUpdate(final Context context, final Handler handler, final int app_or_img) {
        if (getAutoCheckVersion() ) {
            new Thread() {
                /* class com.gzgamut.smart_movement.helper.VersionHelper.AnonymousClass1 */

                public void run() {
                    String type;
                    String currentVersion = null;
                    String serviceVersion = null;
                    if (app_or_img == 1) {
                        if (!DeviceHelper.isSH09() && !DeviceHelper.isSH07() && !DeviceHelper.isSH09u() && !DeviceHelper.isI8() && !DeviceHelper.isHMFitness()) {
                            String currentVersion2 = VersionHelper.getVersionWatch();
                            if (currentVersion2 != VersionHelper.DEF_VERSION_CODE_WATCH) {
                                ImgVersion imgVersion = VersionHelper.parserImgVersion(currentVersion2);
                                if (imgVersion != null && (type = imgVersion.getType()) != null) {
                                    if (type.equalsIgnoreCase("SH09U")) {
                                        if (DeviceHelper.isDevice09U_1_NX()) {
                                            serviceVersion = VersionHelper.getVersionFromServer(Global.URL_IMG_VERSION_SH09U_NX);
                                        } else if (DeviceHelper.isDevice09U_1_3X()) {
                                            serviceVersion = VersionHelper.getVersionFromServer(Global.URL_IMG_VERSION_SH09U_1_3X);
                                        } else {
                                            return;
                                        }
                                    } else if (type.equalsIgnoreCase("I10")) {
                                        if (DeviceHelper.isDeviceI10_1_NX()) {
                                            serviceVersion = VersionHelper.getVersionFromServer(Global.URL_IMG_VERSION_I10_1_NX);
                                        } else if (DeviceHelper.isDeviceI10_1_3X()) {
                                            serviceVersion = VersionHelper.getVersionFromServer(Global.URL_IMG_VERSION_I10_1_3X);
                                        } else {
                                            return;
                                        }
                                    } else if (!type.equalsIgnoreCase("I7+")) {
                                        return;
                                    } else {
                                        if (DeviceHelper.isDeviceI7P_1_NX()) {
                                            serviceVersion = VersionHelper.getVersionFromServer(Global.URL_IMG_VERSION_I7P_1_NX);
                                        }
                                    }
                                    ImgVersion imgVersionCurrent = VersionHelper.parserImgVersion(currentVersion2);
                                    ImgVersion imgVersionService = VersionHelper.parserImgVersion(serviceVersion);
                                    if (!(imgVersionCurrent == null || imgVersionService == null || !CalendarHelper.isDate1AfterDate2(imgVersionService.getDate(), imgVersionCurrent.getDate()))) {
                                        Message msg = new Message();
                                        msg.what = 1111;
                                        msg.obj = serviceVersion;
                                        handler.sendMessage(msg);
                                    }
                                } else {
                                    return;
                                }
                            }
                        } else {
                            return;
                        }
                    } else if (app_or_img == 0) {
                        try {
                            currentVersion = VersionHelper.getVersionName(context);
                            serviceVersion = VersionHelper.getVersionFromServer(Global.URL_APK_VERSION);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (currentVersion == null || serviceVersion == null) {
                            VersionHelper.checkVersionUpdate(context, handler, 1);
                        } else if (VersionHelper.isNewVersion(currentVersion, serviceVersion)) {
                            Message msg2 = new Message();
                            msg2.what = 111;
                            msg2.obj = serviceVersion;
                            handler.sendMessage(msg2);
                        } else {
                            VersionHelper.checkVersionUpdate(context, handler, 1);
                        }
                    }
                    super.run();
                }
            }.start();
        }
    }
}
